package de.argh.codebook.service.mock;

import java.util.ArrayList;
import java.util.List;

import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import de.argh.codebook.model.Coder;
import de.argh.codebook.service.CoderService;

@Service
public class CoderServiceMock implements CoderService {

  private static List<Coder> coders;

  static {
    coders = new ArrayList<Coder>();

    for (int i = 1; i <= 10; i++) {
      coders.add(createCoder(Long.valueOf(i)));
    }
  }

  public Coder getCoderById(Long id) {
    for (Coder coder : coders) {
      if (coder.getId().equals(id)) { 
        return coder;
      }
    }
    return null;
  }

  public List<Coder> getFollower(Long id) {
    List<Coder> follower = new ArrayList<Coder>();
    for (Long followerId = Long.valueOf(1); followerId <= id; followerId++) {
      if (!followerId.equals(id)) {
        follower.add(getCoderById(followerId));
      }
    }

    return follower;
  }

  public Coder getCoderByPrincipalName(String name) throws UsernameNotFoundException {
    for (Coder coder : coders) {
      if (coder.getName().equals(name)) { return coder; }
    }
    throw new UsernameNotFoundException("Coder not found");
  }

  /**
   * Creates an fake coder.
   * 
   * @param id
   * @return
   */
  private static Coder createCoder(Long id) {
    Coder coder = new Coder();

    coder.setId(id);
    coder.setName("Foo" + id);
    coder.setSurname("Bar" + id);
    coder.setEmail("Foo" + id + "@domain.locale");

    return coder;
  }
}
